﻿using System.Data.Entity;
using System.Linq;
using PmSadis.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace PmSadis.Dal
{
    /// <summary>
    /// Контекст моделей
    /// </summary>
    public partial class ModelsContext : DbContext
    {
        /// <summary>
        /// По-умолчанию соединяемся с базой, указанной в строке подключения "ModelsContext"
        /// </summary>
        public ModelsContext()
            : base("ModelsContext")
        {

        }

        /// <summary>
        /// Контекст отчетных месяцев
        /// </summary>
        public DbSet<Month> Monthes { get; set; }

        /// <summary>
        /// Контекст единиц измерения
        /// </summary>
        public DbSet<Unit> Units { get; set; }

        /// <summary>
        /// Контекст элементов содержания
        /// </summary>
        public DbSet<Element> Elements { get; set; }

        /// <summary>
        /// Контекст сообщений службы поддержки
        /// </summary>
        public DbSet<Support> Supports { get; set; }

        /// <summary>
        /// Контекст генконтрактов
        /// </summary>
        public DbSet<GenContract> GenContracts { get; set; }

        /// <summary>
        /// Контекст контрактов
        /// </summary>
        public DbSet<SubContract> SubContracts { get; set; }

        /// <summary>
        /// Контекст объектов содержания
        /// </summary>
        public DbSet<ContractObject> Objects { get; set; }

        /// <summary>
        /// Контекст элементов объектов содержания
        /// </summary>
        public DbSet<ObjectElement> ObjectElements { get; set; }

        /// <summary>
        /// Контекст объектов в контрактах
        /// </summary>
        public DbSet<ObjectInContract> ObjectsInContracts { get; set; }

        /// <summary>
        /// Контекст материалов
        /// </summary>
        public DbSet<Material> Materials { get; set; }

        /// <summary>
        /// Контекст материалов справочных работ
        /// </summary>
        public DbSet<WorkMaterial> WorkMaterials { get; set; }

        /// <summary>
        /// Контекст механизмов
        /// </summary>
        public DbSet<Mechanism> Mechanisms { get; set; }

        /// <summary>
        /// Контекст механизмов справочных работ
        /// </summary>
        public DbSet<WorkMechanism> WorkMechanisms { get; set; }

        /// <summary>
        /// Контекст дорожных рабочих
        /// </summary>
        public DbSet<Worker> Workers { get; set; }

        /// <summary>
        /// Контекст работников справочных работ
        /// </summary>
        public DbSet<WorkWorker> WorkWorkers { get; set; }

        /// <summary>
        /// Контекст машинистов
        /// </summary>
        public DbSet<Driver> Drivers { get; set; }

        /// <summary>
        /// Контекст машинистов справочных работ
        /// </summary>
        public DbSet<WorkDriver> WorkDrivers { get; set; }

        /// <summary>
        /// Контекст справочных работ
        /// </summary>
        public DbSet<Work> Works { get; set; }

        /// <summary>
        /// Контекст работ элемента
        /// </summary>
        public DbSet<ElementWork> ElementWorks { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<GenContract>()
                .HasMany(x => x.SubContracts)
                .WithOptional(x => x.GenContract)
                .HasForeignKey(x => x.GenContractId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<GenContractor>()
                                .Property(e => e.GenContractId)
                                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            modelBuilder.Entity<GenContractor>()
                .HasRequired(x => x.GenContract)
                .WithRequiredDependent(x => x.GenContractor);

            modelBuilder.Entity<SubContractor>()
                .HasMany(x => x.SubContracts)
                .WithOptional(x => x.SubContractor)
                .HasForeignKey(x => x.SubContractorId)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<ObjectInContract>()
                .HasOptional(x => x.SubContract)
                .WithMany(x=>x.Objects)
                .HasForeignKey(x=>x.SubContractId);

            modelBuilder.Entity<Material>()
                .Property(x => x.CostBase)
                .HasColumnType("money");

            modelBuilder.Entity<Mechanism>()
                .Property(x => x.CostBase)
                .HasColumnType("money");

            modelBuilder.Entity<Driver>()
                .Property(x => x.CostBase)
                .HasColumnType("money");

            modelBuilder.Entity<Worker>()
                .Property(x => x.CostBase)
                .HasColumnType("money");

            base.OnModelCreating(modelBuilder);
        }
    }
}
